import avatar from '@/assets/images/avatar.png'

export default {
  install(Vue, options) {
    Vue.directive('lazy', {
      inserted(el, binding) {
        const observer = new IntersectionObserver(([{isIntersecting}]) => {
          if (isIntersecting) {
            observer.unobserve(el)
            el.src = binding.value
            el.addEventListener('error', () => {
              el.src = avatar
            })
          }
        }, {threshold: 0})
        observer.observe(el)
      }
    })
  }
}
